package com.example.mypra1;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import com.example.mypra1.adapter.MyFragmentPagerAdapter;
import com.example.mypra1.fragments.MainFragment1;
import com.example.mypra1.fragments.MainFragment2;
import com.example.mypra1.fragments.MainFragment3;
import com.example.mypra1.fragments.MainFragment4;

import java.util.ArrayList;

/**
 * 用户登录后的app主界面
 */
public class HomeActivity extends AppCompatActivity implements View.OnClickListener {
    private ArrayList<Fragment> fragments;

    ViewPager2 viewPager;
    LinearLayout tab1, tab2, tab3, tab4;//底部标签栏
    ImageView ivTab1, ivTab2, ivTab3, ivTab4,  ivCurrent;//标签图片
    TextView tvTab1, tvTab2, tvTab3, tvTab4,  tvCurrent;//标签文本
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        initPager();
        initView();
        setListener();
        SharedPreferences pref = getSharedPreferences("data",MODE_PRIVATE);
        String username = pref.getString("username","");
        String password = pref.getString("password","");
        String zodiac = pref.getString("zodiac","");
        Log.d("zodiac",zodiac);
        Log.d("username",username);
        Log.d("password",password);//可获取到用户信息，登录功能测试完成
    }
    private void initView() {//获取UI控件
        tab1 = findViewById(R.id.main_tab1);
        tab2 = findViewById(R.id.main_tab2);
        tab3 = findViewById(R.id.main_tab3);
        tab4 = findViewById(R.id.main_tab4);


        ivTab1 = findViewById(R.id.iv_tab1);
        ivTab2 = findViewById(R.id.iv_tab2);
        ivTab3 = findViewById(R.id.iv_tab3);
        ivTab4 = findViewById(R.id.iv_tab4);


        tvTab1 = findViewById(R.id.tv_tab1);
        tvTab2 = findViewById(R.id.tv_tab2);
        tvTab3 = findViewById(R.id.tv_tab3);
        tvTab4 = findViewById(R.id.tv_tab4);

        ivTab1.setSelected(true);
        tvTab1.setSelected(true);
        ivCurrent = ivTab1;
        tvCurrent = tvTab1;
    }
    private void initPager() {//初始化UI控件
        viewPager = findViewById(R.id.viewPager);
        viewPager.setUserInputEnabled(false);
        fragments = new ArrayList<>();
        fragments.add(MainFragment1.newInstance());
        fragments.add(MainFragment2.newInstance());
        fragments.add(MainFragment3.newInstance());
        fragments.add(MainFragment4.newInstance());
        MyFragmentPagerAdapter pagerAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager(), getLifecycle(), fragments);
        viewPager.setAdapter(pagerAdapter);
        viewPager.setCurrentItem(0,false);
        viewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                changeTab(position);
            }
        });
    }
    private void changeTab(int position) {//实现点击标签切换模块时的高亮
        ivCurrent.setSelected(false);
        tvCurrent.setSelected(false);
        switch (position) {
            case R.id.main_tab1:
                viewPager.setCurrentItem(0, false);
            case 0:
                ivTab1.setSelected(true);
                tvTab1.setSelected(true);
                ivCurrent = ivTab1;
                tvCurrent = tvTab1;
                break;
            case R.id.main_tab2:
                viewPager.setCurrentItem(1, false);
            case 1:
                ivTab2.setSelected(true);
                tvTab2.setSelected(true);
                ivCurrent = ivTab2;
                tvCurrent = tvTab2;
                break;
            case R.id.main_tab3:
                viewPager.setCurrentItem(2, false);
            case 2:
                ivTab3.setSelected(true);
                tvTab3.setSelected(true);
                tvCurrent = tvTab3;
                ivCurrent = ivTab3;
                break;
            case R.id.main_tab4:
                viewPager.setCurrentItem(3, false);
            case 3:
                ivTab4.setSelected(true);
                tvTab4.setSelected(true);
                ivCurrent = ivTab4;
                tvCurrent = tvTab4;
                break;
        }
    }
    private void setListener() {//底部导航栏标签绑定点击事件
        tab1.setOnClickListener(this);
        tab2.setOnClickListener(this);
        tab3.setOnClickListener(this);
        tab4.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        changeTab(view.getId());
    }
}
